define(["jquery","F7","config"],function($,F7,conf){
    // Initialize your app
    var myApp = new Framework7({
        template7Pages: true ,// enable Template7 rendering for Ajax and Dynamic pages
        //pushState:true,
        preloadPreviousPage:false,
        animatePages:false,
        swipeBackPage:false,
        swipeBackPageAnimateShadow:false,
        swipeBackPageAnimateOpacity:false,
        // hideToolbarOnPageScroll:true,
    });
    
    // Export selectors engine
    var $$ = Dom7; 
    
    // Add view
    var mainView = myApp.addView('.view-main', {
        // Because we use fixed-through navbar we can enable dynamic navbar
        dynamicNavbar: true
    });
    // 全局静态图片资源 处理 
    var img_error = function(img_url){
        document.addEventListener("error", function (e) {
        var elem = e.target;
        if (elem.tagName.toLowerCase() === 'img') {
            var src = $(elem).attr('src'); 
            setTimeout(function () {
                (function (src) {
                    if (src != undefined) {
                        src += '';
                        src = src.split(";");
                        src = src[0];
                        var src1 = img_url + '/' + src ;
                        var img1 = new Image();
                        img1.src = src1;
                        img1.onload = function () {
                            $(elem).attr({"src": src1, "data-is_loaded": true});
                        }
                        img1.onerror = function () {
                            $(elem).attr({"src": "./app/images/error.jpg", "data-is_loaded": true});
                        }
                    }
                })(src);
            });
    
        }
        }, true);
    }
    
    img_error(conf.SITE);
    // 全局静态图片资源 处理 结束
    var _history_pop = function(){
        myApp.getCurrentView().history.pop()
    };
    var _close = function(){
        myApp.closeModal();
    };
    var _error = function(e){
        if(!window.DEV){ 
            console.error("MY_APP THROW ERROR:",e);
        }
    };
    var _log = function(e){
        if(!window.DEV){ 
            console.log("MY_APP THROW LOG:",e);
        }
    };
    var _url = function(u){
        return conf.SITE+"/"+u;
    }
    var _back = function(){
       mainView.router.back();  
    };
    var _tpl = function(template,context){
        var compiledTemplate = Template7.compile(template);
        return compiledTemplate(context);
    }
    var _post = function(url,data,cb,param){
        _request("POST",url,data,cb,param);
    };
    var _get = function(url,data,cb,param){
        _request("GET",url,data,cb,param);
    };
    var _request = function(type,url,data,cb,param){
        $.ajax({
        	async: true,
        	url: url,
        	data: data,
        	type: type,
        	dataType: "json",
        	beforeSend: function () { 
        	},
        	complete: function () { 
        	},
        	success: function (d, e, f) {
        	    cb(d);
        	},
        	error: function (xhr, textStatus, errorThrown) {
        	   
        	}
        });
    };
    var load_view = function(path,context){
        context.App = window.App;
        _log(context);
        mainView.router.load({ //加载单独页面page
            url:'app/'+path+'.html',//页面的url
            context:context
        });
    };
    var _component = function(param){
        var path = "";
        if(typeof param == "object"){
            path = param.path;
        }else if(typeof param == "string"){
            path = param;
        }
        if(!path){
            _error("App -> _component -> init -> param -> path is not config");
            return;
        }
        require([path],function(res){
            if(res && typeof res == "object" &&res.popup!=undefined){
                require(["text!"+path+".html"],function(tpl){ 
                    myApp.popup(tpl);
                });
            }else{ 
                if(res === undefined){
                    return;
                }
                if(!res || !res.context){
                    context = {}
                }else{
                    context=res.context;
                }
                load_view(path,context); 
                _log("_component load :  "+path);
            }
        },function(){
            _error(path+"not found");
            return ;
        });
    }; 
    
    var _hash = function(key){
        function getHashParameters(){
            var arr = (location.hash || "").replace(/^\#/,'').split("&");
            var params = {};
            for(var i=0; i<arr.length; i++){
                var data = arr[i].split("=");
                if(data.length == 2){
                     params[data[0]] = data[1];
                }
            }
            return params;
        }
        var params = getHashParameters();
        return params[key];
    }
    var input = function(e){
        var param  = _param("param");
        if(typeof param == "string"){
            param = JSON.parse( decodeURI(param)); 
            for(i in param){
                if(i == e){
                    return param[i];
                }
            }
        }
        return false;
    }
    var _param = function(key){
        /**
         * 获取请求参数列表
         * @returns {{}}
         */
        function getRequestParameters(key){
            var arr = (location.search || "").replace(/^\?/,'').split("&");
            var params = {};
            for(var i=0; i<arr.length; i++){
                var data = arr[i].split("=");
                if(data.length == 2){
                    params[data[0]] = data[1];
                }
            }
            return params;
        }
        var params = getRequestParameters();
        return params[key];
    }

    function toast_(str){
        var modal = myApp.modal({
            afterText:  '<div id="toast_modal">'+
            '<p style="text-align:center;color:#fff;margin:0;" >'+str+'</p>'+
            '</div>'
        })
        if ($("#toast_modal").length==1) {
            $('.modal-overlay').css("background","transparent")
            $("#toast_modal").parent().parent().css({"background":"transparent","width":"70%","margin-left":"-35%","margin-top":"60%"})
            $("#toast_modal").parent().css({'background':'rgba(0,0,0,0.8)'})
        }
        setTimeout(function () {
            myApp.closeModal(modal);
            $('.modal-overlay').css("background","rgba(0,0,0,.4)");
            return true;
        }, 2*1000);
    }
    return  window.App = {
        conf:conf,
        myApp:myApp, // Framework7
        $$:$$,  // DOM7
        mainView:mainView,
        tpl:_tpl, //Template7
        history_pop:_history_pop,
        close:_close,
        component:_component, // component
        url:_url,
        back:_back,
        request:_request,
        post:_post,
        get:_get,
        log:_log,
        error:_error,
        hash:_hash,
        toast:toast_,
        param:_param,
        input:input,
        load_view:load_view
    };

});